<template>
<div>
    <el-form size="small" :model="inputForm" ref="inputForm" v-loading="loading" :disabled="formReadOnly"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="24">
            <el-form-item label="学院" prop="college"
                :rules="[
                 ]">
              <el-select v-model="inputForm.college" placeholder="请选择学院" style="width: 100%;" :disabled="collegeDisabled">
                  <el-option
                    v-for="item in officeTreeData"
                    :key="item.id"
                    :label="item.name"
                    :value="item.name">
                  </el-option>
                </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="学年" prop="year"
                :rules="[
                 ]">
              <el-select v-model="inputForm.year" placeholder="请选择学年" style="width: 100%;" :disabled="yearDisabled || (thetitle === '审批[学工处管理员]' || thetitle === '审批【学工处管理员】')">
                  <el-option
                    v-for="(item, index) in yearList"
                    :key="index"
                    :label="item"
                    :value="item">
                  </el-option>
                </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="班级" prop="classes"
                :rules="[
                 ]">
              <el-select v-model="inputForm.classes" placeholder="请选择班级" style="width: 100%;" :disabled="classesDisabled || (thetitle === '审批[学工处管理员]' || thetitle === '审批【学工处管理员】')">
                  <el-option
                    v-for="(item, index) in classesList"
                    :key="index"
                    :label="item.classe"
                    :value="item.classe">
                  </el-option>
                </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="得分" prop="store"
                :rules="[
                 ]">
              <el-input v-model="inputForm.store" placeholder="请填写得分"    disabled></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"   :disabled="isDisabled"  ></el-input>
           </el-form-item>
          <el-button style="margin-left: 5%;" type="text" size="small" @click="refreshList()">查看详情</el-button>
        </el-col>
        <el-col>
          <detailsForm ref="detailsForm" v-show="isSee" style="height: 500px;"></detailsForm>
        </el-col>
        </el-row>
    </el-form>
</div>
</template>

<script>
  import detailsForm from './detailsForm.vue'
  export default {
    data () {
      return {
        officeTreeData: [], // 学院表
        titleName: '',
        thetitle: '',
        isSee: false,
        title: '',
        method: '',
        loading: false,
        collegeDisabled: false,
        yearDisabled: true,
        classesDisabled: true,
        yearList: [], // 学年表
        classesList: [], // 班级表
        isDisabled: false,
        inputForm: {
          id: '',
          college: '',
          classes: '',
          name: '',
          year: '',
          store: '',
          remarks: ''
        }
      }
    },
    props: {
      businessId: {
        type: String,
        default: ''
      },
      formReadOnly: {
        type: Boolean,
        default: false
      }
    },
    components: {
      detailsForm
    },
    watch: {
      'thetitle': {
        handler (newVal) {
          this.thetitle = newVal
          if (newVal === '审批[学工处管理员]' || newVal === '审批【学工处管理员】') {
            this.collegeDisabled = true
          }
        },
        immediate: true,
        deep: false
      },
      'inputForm.year': {
        handler (newVal) { // 请求班级
          if (newVal !== '') {
            this.classesDisabled = false
            this.$http({
              url: `/commit/studentSubmit/downlist`,
              method: 'get',
              params: {
                ...this.inputForm
              }
            }).then(({data}) => {
              this.classesList = data.downlist
            })
          }
        },
        immediate: true,
        deep: false
      },
      'inputForm.college': {
        handler (newVal) { // 请求年级
          if (newVal !== '') {
            this.yearDisabled = false
            this.$http({
              url: `/commit/studentSubmit/gradelist`,
              method: 'get',
              params: {
                'yuan': this.inputForm.college
              }
            }).then(({data}) => {
              this.yearList = data.gradelist
            })
          }
        },
        immediate: true,
        deep: false
      },
      'businessId': {
        handler (newVal) {
          if (this.businessId) {
            this.init(this.businessId)
          } else {
            this.$nextTick(() => {
              this.$refs.inputForm.resetFields()
            })
          }
        },
        immediate: true,
        deep: false
      }
    },
    mounted () {
      this.refreshTree()
      this.thetitle = this.$route.query.title
    },
    methods: {
      refreshTree () {
        this.$http({ // 请求学院
          url: `/sys/office/treeData`,
          method: 'get'
        }).then(({data}) => {
          this.officeTreeData = data.treeData[0].children
        })
      },
      refreshList () {
        this.isSee = true
        this.$refs.detailsForm.refreshList(this.inputForm.college, this.inputForm.year, this.inputForm.classes)
        this.$http({
          url: '/moredumain/studentMoreduMain/countPoint',
          method: 'get',
          params: {
            'classes': this.inputForm.classes
          }
        }).then(({data}) => {
          this.inputForm.store = data
        })
      },
      init (id) {
        if (id) {
          this.loading = true
          this.inputForm.id = id
          this.$nextTick(() => {
            this.$refs.inputForm.resetFields()
            this.$http({
              url: `/commit/studentSubmit/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.studentSubmit)
              this.loading = false
            })
          })
        }
      },
      // 表单提交
      saveForm (callback) {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/commit/studentSubmit/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.success) {
                callback(data.businessTable, data.businessId)
              }
            })
          }
        })
      }
    }
  }
</script>
